import {Component, OnInit, Input, AfterViewInit, OnDestroy} from '@angular/core';
import {Project} from "../entity/Project";
import {ProjectAddService} from "../service/project.add.service";
import {CategoryFindService} from "../../category/service/category.find.service";
import {Category} from "../../category/entity/Category";
import {TreeUtils} from "../../shared/utils/TreeUtils";
import {TreeEntity} from "../../shared/entity/TreeEntity";
import {CommonMsg} from "../../shared/entity/CommonMsg";
import {Router} from "@angular/router";
declare var $: any;
@Component({
  selector: 'project-add',
  templateUrl: './project-add.html',
  styleUrls: ['./project-add.css']
})

export class ProjectAddComponent
implements OnInit
{

  public editor;
  public project:Project = new Project();
  public categoryList:Array<Category> = new Array<Category>();
  public tree:Array<TreeEntity>;
  public treeUtil:TreeUtils  = new TreeUtils();
  public agreeSelected:boolean =true;

  ngOnInit(): void {
    this.categoryFindService.findCategoryTreeData()
      .then(categoryList =>{
        this.tree = this.treeUtil.sonsTree(categoryList,1);
      });
  }
  constructor(private projectService: ProjectAddService
    ,private categoryFindService:CategoryFindService
    , private router: Router
  ) {

  }


  onSubmit() {
    /* var content=   document.getElementById("myeditor").innerHTML;
     console.log("content:"+content);*/
    var projectCategoryId =  $("#categoryId").val();
    this.project.category.id = projectCategoryId;
    this.project.createBy.id = sessionStorage.getItem("id");


    if(this.project.title==null||this.project.title==''){
      $.jBox.tip("请填写标题!");
      return;
    }
    if(this.project.category.id==null||this.project.category.id==''){
      $.jBox.tip("请选择项目分类!");
      return;
    }
    if(this.project.description==null||this.project.description==''){
      $.jBox.tip("请填写项目描述!");
      return;
    }
    if(this.project.isOpenItem==null||this.project.isOpenItem==''){
      $.jBox.tip("请选择公开性!");
      return;
    }
    if(this.agreeSelected==false){
      $.jBox.tip("同意协议后,才能发布项目!");
      return;
    }
    // if(!document.getElementById("checkboxID").checked){
    //   $.jBox.tip("同意协议之后才能发布项目!");
    // }
   if(this.project.title.length>20){
     $.jBox.tip("您输入的项目名称太长，请保持在20字内");
     return;
   }
   if(this.project.description.length>200){
     $.jBox.tip("您输入的项目描述太长，请保持在200字内");
   }
    this.projectService.publishProject(this.project).then(commonMsg=>
    {
      this.handleResult(commonMsg);
    }
    );
  }
  public handleResult(commonMsg:CommonMsg<any>){
    if(commonMsg!=null&&commonMsg.code==200){
      $.jBox.tip("项目发布成功!");
      this.project = commonMsg.entity;

      this.router.navigate(['/project/index/',this.project.id]);
    }else{
      $.jBox.tip("项目发布失败!");
    }
  }

  onClickMe(){




    $.jBox("id:box", {
      title: "选择分类信息",
      width: 300,
      height: 420,
      buttons:{"确定":"ok","关闭":"cancel"},
      submit:function (v, h, f) {
        if(v=="cancel"){
          $.jBox.tip("取消选择");
        }
        if(v=="ok")
        {
          var name = h.find('input[name="category-info"]:checked').val();
          var data = name.split(',');
          $("#categoryName").val(data[0]);
          $("#categoryId").val(data[1]);
        }
      },loaded:function(h){
        $(".jbox-content",top.document).css("overflow-y","hidden");
      }

    });
  }
  public showFormControls(form: any) {
    return form && form.controls['name'] &&
      form.controls['name'].value; // Dr. IQ
  }

}
